<?php
	/* Functions in this file */
	/**************************/

	// ClearFlag($flag)
	// GetKeyAccidentals()
	// GetKeyType()
	// GetMFCClassName()
	// GetMFCClassSchema()
	// HasFiveFlats()
	// HasFiveSharps()
	// HasFourFlats()
	// HasFourSharps()
	// HasNoKeyAccidentals()
	// HasOneFlat()
	// HasOneSharp()
	// HasSevenFlats()
	// HasSevenSharps()
	// HasSixFlats()
	// HasSixSharps()
	// HasThreeFlats()
	// HasThreeSharps()
	// HasTwoFlats()
	// HasTwoSharps()
	// Hide()
	// IsCancellation()
	// IsFlagSet($flag)
	// IsMajorKey()
	// IsMinorKey()
	// IsShown()
	// IsValidFlag($flag)
	// IsValidKeyAccidentals($keyAccidentals)
	// IsValidKeyType($keyType)
	// SetFlag($flag)
	// SetKeyAccidentals($keyAccidentals)
	// SetKeyType($keyType)
	// Show()
?>
 
<?php
	 
	class KeySignature {
		 
		 
		// Default constants
		const DEFAULT_DATA = 0;
		 
		 
		const keyAccidentalsMask = 0x0000000f;
		const show = 0x00000010;
		const cancellation = 0x00000020;
		const keyTypeMask = 0x00000040;
		 
		 
		 
		const majorKey = 0;
		const minorKey = 1;
		 
		 
		 
		const noAccidentals = 0;
		const oneSharp = 1;
		const twoSharps = 2;
		const threeSharps = 3;
		const fourSharps = 4;
		const fiveSharps = 5;
		const sixSharps = 6;
		const sevenSharps = 7;
		const oneFlat = 8;
		const twoFlats = 9;
		const threeFlats = 10;
		const fourFlats = 11;
		const fiveFlats = 12;
		const sixFlats = 13;
		const sevenFlats = 14;
		 
		 
		// Member variables
		 
		protected $m_data = self::DEFAULT_DATA;
		 
		 
		// MFC Class Functions
		 
		 
		function GetMFCClassName() {
			return "CKeySignature";
		}
		 
		 
		function GetMFCClassSchema() {
			return 1;
		}
		 
		// Serialization Functions
		 
		 
		 
		function IsSameKey(&$keySignature) {
			return (($this->GetKeyType() == $keySignature->GetKeyType()) && ($this->GetKeyAccidentals() == $keySignature->GetKeyAccidentals()));
		}
		 
		// Type Functions
		 
		 
		 
		function IsValidKeyType($keyType) {
			return (($keyType == self::majorKey) || ($keyType == self::minorKey));
		}
		 
		function GetKeyType() {
			return (($this->m_data & self::keyTypeMask) >> 6);
		}
		 
		 
		function IsMajorKey() {
			return ($this->GetKeyType() == self::majorKey);
		}
		 
		 
		function IsMinorKey() {
			return ($this->GetKeyType() == self::minorKey);
		}
		 
		// Accidentals Functions
		 
		 
		 
		function IsValidKeyAccidentals($keyAccidentals) {
			return ($keyAccidentals <= self::sevenFlats);
		}
		 
		 
		function HasNoKeyAccidentals() {
			return ($this->GetKeyAccidentals() == self::noAccidentals);
		}
		 
		 
		function HasOneSharp() {
			return ($this->GetKeyAccidentals() == self::oneSharp);
		}
		 
		 
		function HasTwoSharps() {
			return ($this->GetKeyAccidentals() == self::twoSharps);
		}
		 
		 
		function HasThreeSharps() {
			return ($this->GetKeyAccidentals() == self::threeSharps);
		}
		 
		 
		function HasFourSharps() {
			return ($this->GetKeyAccidentals() == self::fourSharps);
		}
		 
		 
		function HasFiveSharps() {
			return ($this->GetKeyAccidentals() == self::fiveSharps);
		}
		 
		 
		function HasSixSharps() {
			return ($this->GetKeyAccidentals() == self::sixSharps);
		}
		 
		 
		function HasSevenSharps() {
			return ($this->GetKeyAccidentals() == self::sevenSharps);
		}
		 
		 
		function HasOneFlat() {
			return ($this->GetKeyAccidentals() == self::oneFlat);
		}
		 
		 
		function HasTwoFlats() {
			return ($this->GetKeyAccidentals() == self::twoFlats);
		}
		 
		 
		function HasThreeFlats() {
			return ($this->GetKeyAccidentals() == self::threeFlats);
		}
		 
		 
		function HasFourFlats() {
			return ($this->GetKeyAccidentals() == self::fourFlats);
		}
		 
		 
		function HasFiveFlats() {
			return ($this->GetKeyAccidentals() == self::fiveFlats);
		}
		 
		 
		function HasSixFlats() {
			return ($this->GetKeyAccidentals() == self::sixFlats);
		}
		 
		 
		function HasSevenFlats() {
			return ($this->GetKeyAccidentals() == self::sevenFlats);
		}
		 
		// $this->Show/$this->Hide Functions
		 
		function Show() {
			$this->SetFlag(self::show);
		}
		 
		function Hide() {
			$this->ClearFlag(self::show);
		}
		 
		 
		function IsShown() {
			return ($this->IsFlagSet(self::show));
		}
		 
		// Cancellation Functions
		 
		 
		function SetCancellation($set = 1) {
			if ($set) $this->SetFlag(self::cancellation);
				 else $this->ClearFlag(self::cancellation);
		}
		 
		 
		function IsCancellation() {
			return ($this->IsFlagSet(self::cancellation));
		}
		 
		// Flag Functions
		 
		 
		 
		 
		function IsValidFlag($flag) {
			return (($flag == self::show) || ($flag == self::cancellation));
		}
		 
		 
		function SetFlag($flag) {
			if (!$this->IsValidFlag($flag)) return 0;
			$this->m_data |= $flag;
			 return 1;
		}
		 
		 
		function ClearFlag($flag) {
			if (!$this->IsValidFlag($flag)) return 0;
			$this->m_data &= ~$flag;
			 return 1;
		}
		 
		 
		 
		function IsFlagSet($flag) {
			if (!$this->IsValidFlag($flag)) return 0;
			 return (($this->m_data & $flag) == $flag);
		}
		 
		 
		 
		 
		function KeySignature($keyType = NULL, $keyAccidentals = NULL) {
			 
			 
			$this->SetKey($keyType, $keyAccidentals);
		}
		 
		
		// Serialize Functions
		 
		 
		 
		function DoSerialize(&$stream){
		 
			WRITE_BYTE ($stream,$this->m_data);
			return 1;
		}
		 
		 
		 
		 
		function DoDeserialize(&$stream){
		 
		
			$this->m_data = READ_BYTE ($stream);
		
		}
		
		// Key Functions
		 
		 
		 
		function SetKey($keyType, $keyAccidentals) {
			 
			//------Last Checked------//
			// - Dec 11, 2004
			if (!$this->SetKeyType($keyType))
				return 0;
			if (!$this->SetKeyAccidentals($keyAccidentals))
				return 0;
			return 1;
		}
		 
		 
		 
		 
		function GetKey(&$keyType, &$keyAccidentals) {
			 
			//------Last Checked------//
			// - Dec 11, 2004
			$keyType = $this->GetKeyType();
			$keyAccidentals = $this->GetKeyAccidentals();
			 
			// Cancellations store the key of the cancelled type, but we know that the cancellations will always
			// occur on C Major/A Minor so $set the key to 0
			if ($this->IsCancellation())
				$keyAccidentals = 0;
		}
		 
		 
		 
		 
		 
		function GetDrawKey(&$keyType, &$keyAccidentals) {
			 
			//------Last Checked------//
			// - Dec 11, 2004
			$keyType = $this->GetKeyType();
			$keyAccidentals = ($this->m_data & self::keyAccidentalsMask);
			return ($this->IsCancellation());
		}
		 
		// Key Type Functions
		 
		 
		 
		function SetKeyType($keyType) {
			 
			//------Last Checked------//
			// - Dec 11, 2004
			if (!$this->IsValidKeyType($keyType)) return 0;
			$this->m_data &= ~self::keyTypeMask;
			$this->m_data |= ($keyType << 6);
			return 1;
		}
		 
		// Key Accidentals Functions
		 
		 
		 
		function SetKeyAccidentals($keyAccidentals) {
			 
			//------Last Checked------//
			// - Dec 11, 2004
			if (!$this->IsValidKeyAccidentals($keyAccidentals)) return 0;
			$this->m_data &= ~self::keyAccidentalsMask;
			$this->m_data |= $keyAccidentals;
			return 1;
		}
		 
		 
		 
		function GetKeyAccidentals() {
			 
			//------Last Checked------//
			// - Dec 10, 2004
			$returnValue = ($this->m_data & self::keyAccidentalsMask);
			 
			// Cancellations store the key of the cancelled type, but we know that the cancellations will always
			// occur on C Major/A Minor so $set the key to 0
			if ($this->IsCancellation())
				$returnValue = 0;
			return ($returnValue);
		}
		 
		 
	}
	 
?>

